package com.blog.cloud.core.comfig;

import com.blog.cloud.core.properties.ThreadPoolProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@ConditionalOnProperty(prefix = "thread.pool", value = "enabled", havingValue = "true", matchIfMissing = false)
@EnableConfigurationProperties(value = {ThreadPoolProperties.class})
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolProperties properties) {
        return new ThreadPoolExecutor(
                properties.getCorePoolSize(),
                properties.getMaximumPoolSize(),
                properties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(properties.getQueueCapacity()));
    }
}

// 为了让线程池的配置数据能够从配置文件中批量导入，我们可以创建一个映射类并使用配置文件处理器完成
// 例如：创建线程池配置文件 ThreadPoolProperties
